//
//  ZTTabBar.m
//  DianYing5
//
//  Created by d0 on 15/7/17.
//  Copyright (c) 2015年 uskr. All rights reserved.
//

#import "ZTTabBar.h"
#import "ZTTabBarButton.h"

@interface ZTTabBar ()

@property (nonatomic, weak)ZTTabBarButton * selectedButton;

@end

@implementation ZTTabBar

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self)
    {
        self.backgroundColor = [UIColor whiteColor];
        
        //添加按钮
        [self addButton];
    }
    return self;
}

-(void)addButton
{

    NSArray *buttonCheckedImage = @[@"tab_Send_Select",
                                    @"tab_Grab_Select",
                                    @"tab_Scan_Select",
                                    @"tab_Notice_Select",
                                    @"tab_User_Select"];
    
    NSArray *buttonUncheckedImage = @[@"tab_Send_Normal",
                                      @"tab_Grab_Normal",
                                      @"tab_Scan_Normal",
                                      @"tab_Notice_Normal",
                                      @"tab_User_Normal"];
    
    NSArray *buttonTitle = @[@"派件",@"抢单", @"扫描",@"通知",@"我的"];
    
    for(int i = 0; i < 5; i++)
    {
        //在按钮初始化时,内部做了label的init,和两个setImage;
        __weak ZTTabBar* weakSelf = self;
        
        ZTTabBarButton * btn = [[ZTTabBarButton alloc]
                                    initWithLabelRect:CGRectMake(0, 35, [UIScreen mainScreen].bounds.size.width/5, 20)
                                    withSelectedImage:[UIImage imageNamed:buttonCheckedImage[i]]
                                    withUnselectImage:[UIImage imageNamed:buttonUncheckedImage[i]]
                                  withImageEdgeInsets:UIEdgeInsetsMake(3, 22, 20, 22)
                                           withAction:^(ZTTabBarButton *button){//点击事件BLOCK

                    weakSelf.selectedButton.label.textColor = [UIColor lightGrayColor];

                    button.label.textColor = [UIColor whiteColor];

                    weakSelf.selectedButton.selected = NO;
                       
                    button.selected = YES;
                       
                    weakSelf.selectedButton = button;
                       
                    if(weakSelf.selectBlock)
                    {
                        weakSelf.selectBlock(button.tag);
                    }
                }];


        //对Button中的属性做额外的设置
        btn.tag = i;
        
        btn.label.text = buttonTitle[i];
        
        btn.label.font = [UIFont boldSystemFontOfSize:14];
        
        btn.label.textColor = [UIColor lightGrayColor];
        
        [self addSubview:btn];
        
        //第一次运行时,显示第一个页面和第一个按钮,将这个按钮点击一下
        if(i == 0)
        {
            //[self btnClick:btn];
            if(btn.action)
                btn.action(btn);
        }
    }
}


//在这里做按钮的宽高及布局
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnW = self.bounds.size.width / self.subviews.count;
    
    CGFloat btnH = self.bounds.size.height;
    
    CGFloat btnX = 0;
    
    CGFloat btnY = 0;
    
    //设置按钮的尺寸
    for(int i = 0; i < self.subviews.count; i++)
    {
        UIButton * btn = self.subviews[i];
        
        btnX = i * btnW;
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}
@end
